package top.kaoshanji.p1ch11a;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.util.Date;

/**
 * 代码清单-p1ch11a-370页a：指定显示
 * URL访问地址：/specialGetter
 * @author kaoshanji
 * ^_^
 * create time 2025/4/5 14:45
 */
public class SpecialGetter extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求对象的编码
        req.setCharacterEncoding(StandardCharsets.UTF_8.name());

        // 设置响应对象的编码和内容格式
        resp.setContentType(ContentType.TEXT_HTML.toString());
        PrintWriter out = resp.getWriter();

        ServletContext context = getServletContext();
        String burrito = (String) context.getAttribute("com.costena.special.burrito");
        Date day = (Date) context.getAttribute("com.costena.special.day");

        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        String today = df.format(day);

        out.println("Our burrito special today (" + today + ") is: " + burrito);

    }

}
